﻿using Raffles.OA.Utility;
using Raffles.OA.Data;
using Mapster;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace Raffles.OA.WebApp.Areas.Manage.Controllers
{
    public class DictionaryController : BaseController<Dictionary>
    {
        /// <summary>
        /// 获取字典类型分页列表
        /// </summary>
        /// <param name="param"></param>
        /// <param name="pagination"></param>
        /// <returns></returns>
        public ActionResult GetPageListJson(BaseSearchParam param, Pagination pagination)
        {
            int totalCount = 0;
            var dictionarys = _dbContext.Queryable<Dictionary>().Where(x => !x.DeletedMark)
                                                    .WhereIF(!param.KeyWord.IsEmpty(), x => x.DicType == param.KeyWord.Trim() || x.Description == param.KeyWord.Trim())
                                                    .OrderBy(x => x.DisplayOrder)
                                                    .ToPageList(pagination.PageIndex, pagination.PageSize, ref totalCount);
            return Content(DataResult<List<Dictionary>>.PageResult(dictionarys, totalCount).ToJson());
        }

        public ActionResult Form(int? id)
        {
            Dictionary dictionary = new Dictionary();
            if (id != null)
            {
                dictionary = _dbContext.Queryable<Dictionary>().InSingle(id);
            }
            return View(dictionary);
        }

        /// <summary>
        /// 字典保存
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public ActionResult SaveForm(DictionaryDto dictionaryDto)
        {
            try
            {
                if (dictionaryDto.Id == null)
                {
                    if (_dbContext.Queryable<Dictionary>().Any(x => !x.DeletedMark && x.DicType == dictionaryDto.DicType))
                    {
                        return Json(DataResult.Error("字典类型不允许重复"));
                    }
                    Dictionary dictionary = new Dictionary();
                    dictionaryDto.Adapt(dictionary);
                    dictionary.CreatedBy = OperatorProvider.Provider.GetCurrent().Id;
                    _dbContext.Insertable<Dictionary>(dictionary).ExecuteCommand();
                    return Json(DataResult.Success("字典新增成功"));
                }
                else
                {
                    if (_dbContext.Queryable<Dictionary>().Any(x => !x.DeletedMark && x.DicType == dictionaryDto.DicType && x.Id != dictionaryDto.Id))
                    {
                        return Json(DataResult.Error("字典类型不允许重复"));
                    }
                    Dictionary dictionary = _dbContext.Queryable<Dictionary>().InSingle(dictionaryDto.Id);
                    dictionaryDto.Adapt(dictionary);
                    dictionary.UpdatedOn = DateTime.Now;
                    dictionary.UpdatedBy = OperatorProvider.Provider.GetCurrent().Id;
                    _dbContext.Updateable<Dictionary>(dictionary).ExecuteCommand();
                    return Json(DataResult.Success("字典编辑成功"));
                }
            }
            catch (Exception ex)
            {
                return Json(DataResult.Error($"保存失败,失败原因:{ex.Message}"));
            }
        }

        /// <summary>
        /// 字典删除
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public ActionResult DeleteData(int id)
        {
            Dictionary dictionary = _dbContext.Queryable<Dictionary>().InSingle(id);
            if (_dbContext.Queryable<DictionaryDetail>().Any(x => !x.DeletedMark && x.DicType == dictionary.DicType))
            {
                return Json(DataResult.Error("该字典存在字典数据,不允许删除"));
            }
            dictionary.DeletedMark = true;
            dictionary.UpdatedOn = DateTime.Now;
            dictionary.UpdatedBy = OperatorProvider.Provider.GetCurrent().Id;
            _dbContext.Updateable<Dictionary>(dictionary).ExecuteCommand();
            return Json(DataResult.Success("删除成功"));
        }

        public ActionResult GetDictionaryList()
        {
            List<Dictionary> dictionaries = _dbContext.Queryable<Dictionary>().Where(x => !x.DeletedMark).OrderBy(x => x.DisplayOrder).ToList();
            return Content(DataResult<List<Dictionary>>.Success(dictionaries).ToJson());
        }

    }
}